package cmdb_blacklist_ignore_v1

import (
	"context"

	cmdb_blacklist_rpc "dfx_cmdb/cmdb-blacklist-rpc/types/cmdb-blacklist-rpc"
	"dfx_cmdb/cmdb-gateway/internal/svc"
	"dfx_cmdb/cmdb-gateway/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetAllIgnoreRuleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetAllIgnoreRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAllIgnoreRuleLogic {
	return &GetAllIgnoreRuleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetAllIgnoreRuleLogic) GetAllIgnoreRule() (resp *types.QueryAccountInfoResponse, err error) {
	// 调用 RPC 服务获取所有忽略规则
	grpcReq := &cmdb_blacklist_rpc.NullMessageRequest{}
	grpcResp, err := l.svcCtx.IgnoreRuleOperation.GetAllIgnoreRule(l.ctx, grpcReq)
	if err != nil {
		return nil, err
	}

	// 转换为 HTTP 响应格式
	var httpRules []types.UpdateIgnoreRuleRequest
	for _, rule := range grpcResp.UpdateIgnoreRuleRequest {
		httpRules = append(httpRules, types.UpdateIgnoreRuleRequest{
			Id:         rule.Id,
			IgnoreType: rule.IgnoreType,
			IgnoreVal:  rule.IgnoreVal,
			Enabled:    rule.Enabled,
		})
	}

	return &types.QueryAccountInfoResponse{
		UpdateIgnoreRuleRequest: httpRules,
	}, nil
}
